package com.ybb.vue.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybb.vue.common.Result;
import com.ybb.vue.common.util.CreateRandOdd;
import com.ybb.vue.domain.dto.GoodsOutAddDto;
import com.ybb.vue.entity.Approval;
import com.ybb.vue.entity.GoodsInventory;
import com.ybb.vue.entity.GoodsOut;
import com.ybb.vue.mapper.GoodsInventoryMapper;
import com.ybb.vue.service.ApprovalService;
import com.ybb.vue.service.GoodsOutService;
import com.ybb.vue.mapper.GoodsOutMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author apple
* @description 针对表【goods_out(防疫物资出库)】的数据库操作Service实现
* @createDate 2023-04-04 16:53:40
*/
@Service
public class GoodsOutServiceImpl extends ServiceImpl<GoodsOutMapper, GoodsOut>
implements GoodsOutService{

    @Autowired
    private GoodsInventoryMapper goodsInventoryMapper;

    @Autowired
    private ApprovalService approvalService;

    @Override
    public Result out(GoodsOutAddDto goodsOutAddDto) {
        GoodsInventory goodsInventory = goodsInventoryMapper.selectById(goodsOutAddDto.getId());
        if (goodsInventory.getStock() < goodsOutAddDto.getOutNumber()) {
            return Result.error("-1", "库存不足");
        }
        GoodsOut goodsOut = new GoodsOut();
        BeanUtils.copyProperties(goodsOutAddDto, goodsOut);
        goodsOut.setOutOdd(CreateRandOdd.createRandOdd());
        goodsOut.setOutStatus("0");
        goodsOut.setOutTime(new Date());
        goodsOut.setHandler(SecurityContextHolder.getContext().getAuthentication().getName());
        this.save(goodsOut);

        // 写入审核表
        Approval approval = new Approval();
        approval.setSubjectId(goodsOut.getId());
        approval.setApprovalStatus("0");
        approval.setSubjectSource("OUT");
        approvalService.save(approval);

        // 操作记录
//        String content = "操作了入库单表，【新增】了1条记录，记录id为：" + goodsIn.getId();
//        OperationRecordsUtils.createOperationRecords(content);
        return Result.success();
    }
}
